package eval

import (
	"showen/num"
	"showen/obj"
	"testing"
)

func TestEqualWithConsPairs(t *testing.T) {
	env := obj.NewEnvironment()
	RegisterBuiltinProcedures(env)

	// Test the specific case that was causing a panic:
	// (define x (cons 1 '()))
	// (equal? x '(1))

	// Create a pair with 1 and empty list using cons
	consArgs := []obj.Object{
		&obj.NumberObject{Value: num.Integer{Value: 1}},
		obj.EMPTY_LIST,
	}
	consResult := Cons(consArgs, env)

	// Create a list literal '(1)
	listArgs := []obj.Object{
		&obj.NumberObject{Value: num.Integer{Value: 1}},
	}
	listResult := List(listArgs, env)

	// Compare them with equal?
	equalArgs := []obj.Object{consResult, listResult}
	equalResult := Equal(equalArgs, env)

	// Should return #t since both represent the same list structure
	if equalResult.Type() != obj.BOOLEAN_OBJ {
		t.Fatalf("equal? should return a boolean, got %v", equalResult.Type())
	}

	boolResult := equalResult.(*obj.BooleanObject)
	if !boolResult.Value {
		t.Fatalf("equal? should return #t for (cons 1 '()) and '(1), got #f")
	}
}
